package com.google.code.bigyang.base;

import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.atomic.AtomicInteger;

import com.google.code.bigyang.nio.MyHandler;
import com.google.code.bigyang.nio.NioSocketSession;

public class QueueThreadPool implements ThreadPool,Runnable {

	private LinkedBlockingQueue<Task> queue = new LinkedBlockingQueue<Task>();
	
	private AtomicInteger index = new AtomicInteger(0);
	
	
	public void run() {
		while(true) {
			try {
				Task task = queue.take();
				NioSocketSession session = task.getSession();
				Thread t = new Thread(new MyHandler(session),"hanlder-"+index.incrementAndGet());
				t.start();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}
	
	
	public void schedule(Task task) {
		queue.add(task);
	}
	
	
}
